<?php

// -----------------------------------------------------------------------------

include "config/config.php";
include "DBManager.php";
include "global/globalAuth.php";
include "createDBIfNotExists.php";

$db = new DBManager($dbPath);

// -----------------------------------------------------------------------------

// 设置时区
ini_set('date.timezone', $timezone);
// 取得今日日期
$today = date('Y-m-d');

// -----------------------------------------------------------------------------

if (!isset($_GET['dateCreate']) || !isset($_GET['from'])) {

    exit("需要传入完整的参数。");

}

$from = $_GET['from'];
$dateCreate = $_GET['dateCreate'];
$content = $db->readFromTable($dateCreate);

// -----------------------------------------------------------------------------

$title = "编辑";
$placeHolder = "";
if ($from == "create") {
    $title = "新建";
    $placeHolder = "如果当日已存在记忆片段，自动在末尾追加新创建的内容。";
    $content = "";
}

// -----------------------------------------------------------------------------

?>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title><?php echo $title; ?></title>
    <link rel="stylesheet" href="resources/bootstrap-4.3.1-dist/css/bootstrap.min.css">
</head>
<body>

<!-- ----------------------------------------------------------------------- -->
<?php include 'global/globalNavBar.php'; ?>
<!-- ----------------------------------------------------------------------- -->

<!--container-->
<div class="container">

<!-- ----------------------------------------------------------------------- -->

    <form action="editFormAction.php" method="post" class="form-group mt-2">

        <div>

            <?php if ($from == "edit"): ?>
                <input id="inputFrom" name="from" type="hidden" value="edit">
            <?php else: ?>
                <input id="inputFrom" name="from" type="hidden" value="create">
            <?php endif; ?>

            <div>
                <label for="">初始记忆日期：</label>
            </div>
            <div>
                <input name="dateCreate"
                       id="inputDateCreate"
                       type="date"
                       value="<?php echo $dateCreate; ?>"
                       class="form-control form-control-sm" required>
            </div>
        </div>

        <div class="mt-2">
            <div>
                <label for="">记忆片段：</label>
            </div>
            <div>
                <textarea name="content"
                          id="inputContent"
                          cols="30"
                          rows="15"
                          autofocus
                          placeholder="<?php echo $placeHolder; ?>"
                          class="form-control" ><?php echo $content ?></textarea>
            </div>
        </div>

        <div class="mt-2">
            <button type="submit" class="btn btn-sm btn-outline-primary">保存</button>
            <button type="button" id="btnReset" class="btn btn-sm btn-outline-secondary">清空</button>
        </div>

    </form>

<!-- ----------------------------------------------------------------------- -->

</div>
<!--container-->

<!-- ----------------------------------------------------------------------- -->
<script src="resources/jquery/jquery-3.4.0.min.js"></script>
<script src="resources/bootstrap-4.3.1-dist/js/bootstrap.min.js"></script>
<!-- ----------------------------------------------------------------------- -->

<script>
    // 点击清空按钮时执行的动作
    $('#btnReset').click(function () {
        $('#inputContent').val('');
        $('#inputContent').focus();
    })
</script>

<!-- ----------------------------------------------------------------------- -->

</body>
</html>